<template>
  <el-dialog v-model="visible" :title="form.id ? '修改' : '添加'" @closed="onReset()" :close-on-click-modal="false">
    <el-form :model="form" ref="formRef" label-width="100px">
      <el-form-item label="项目名称">
        <el-input v-model="form.project_name" placeholder="请输入"></el-input>
      </el-form-item>
      <el-form-item label="评估单位">
        <el-input v-model="form.evaluation_unit" placeholder="请输入"></el-input>
      </el-form-item>
      <el-form-item label="评估人员">
        <el-input v-model="form.evaluation_user" placeholder="请输入"></el-input>
      </el-form-item>
      <el-form-item label="评估时间">
        <el-date-picker v-model="form.evaluation_date" type="date" placeholder="请选择" value-format="YYYY-MM-DD" />
      </el-form-item>
      <el-form-item label="评估报告">
        <CustomUpload
          v-model="form.evaluation_file"
          :limit="1"
          style="width: 100%;"
          :show-file-list="true"
          :file-list="form.fileList"
        >
          <el-button type="primary">选择文件</el-button>
        </CustomUpload>
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="visible = false">取消</el-button>
        <el-button type="primary" @click="onConfirm()">确定</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script lang="ts" setup>
import { reactive, ref } from 'vue'
import { pinggubaogaoAddOrUpdate } from '@/api/pinggubaogao'
import { ElMessage } from 'element-plus'
import CustomUpload from '@/components/CustomUpload/index.vue'
const emit = defineEmits(['refresh'])
const visible = ref(false)
const formRef = ref()
const form = reactive({
  id: 0,
  project_name: '',
  evaluation_unit: '',
  evaluation_user: '',
  evaluation_date: '',
  evaluation_file: '',
  fileList: []
})
const resetForm = JSON.parse(JSON.stringify(form))

const open = (data) => {
  visible.value = true
  form.fileList = []
  if (data) {
    form.fileList = [{ name: data.evaluation_file, url: data.evaluation_file }]
    Object.assign(form, data)
  }
}
const onConfirm = async () => {
  await formRef.value.validate()
  pinggubaogaoAddOrUpdate(form).then(({ msg }: any) => {
    ElMessage.success(msg)
    visible.value = false
    emit('refresh')
  })
}
const onReset = () => {
  console.log(resetForm)
  Object.keys(resetForm).forEach((item: string) => {
    form[item] = resetForm[item]
  })
  console.log(form)
}
defineExpose({ open })
</script>

<style lang="scss" scoped></style>
